﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.Models;
using PBOffice.BLL;

public partial class ScheduleManager_MySchedule : System.Web.UI.Page
{
    List<MeetingInfo> meetingInfo = new MeetingInfoManager().GetAllMeetingInfo();
    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnDelete.Enabled = false;
        this.ddlType.DataSource = meetingInfo;
        this.ddlType.DataBind();
         ListItem ll = new ListItem("请选择");
        this.ddlType.Items.Insert(0, ll);
        string day = Request.QueryString["day"];
        string month = Request.QueryString["month"];
        string year = Request.QueryString["year"];
        this.yuyue.Attributes.Add("onclick", "imgClik(200,300);");
        // this.lbCreateName.Text = Session["username"].ToString();
        this.lbCreateName.Text = year + "-" + month + "-" + day + " " + DateTime.Now.ToShortTimeString();

        string scheduleId = Request.QueryString["id"];
        if (scheduleId != null)
        {
            this.btnDelete.Enabled = true;
            Schedule schedule = new ScheduleManager().GetScheduleById(Convert.ToInt32(scheduleId));
            this.txtAddress.Text = schedule.Address;
            this.txtBegin.Text = schedule.BeginTime.ToString(); ;
            lbCreateDate.Text = schedule.CreateTime.ToString();
            this.lbCreateName.Text = schedule.CreateUser;
            this.txtEnd.Text = schedule.EndTime.ToString();
            if (schedule.IfPrivate == 0)
            {
                this.cbIfPublic.Checked = true;
            }
            else
            {
                this.cbIfPublic.Checked = false;

            }

            this.ddlType.Text = schedule.Meeting.MeetingName;
            txtContent.Text = schedule.SchContent;
            txtTitle.Text = schedule.Title;
            List<PreContract> preList = new PreContractManager().GetPreContractByScheduleId(Convert.ToInt32(scheduleId));
            this.ddlType.Items.Clear();
            foreach (PreContract item in preList)
            {
                string dept = new StaffManager().GetDeptNameByName(item.UserId);
                ListItem eee = new ListItem();
                eee.Text = dept + "-->" + item.UserId;
                eee.Value = item.PreContractId.ToString();
                this.lbStfName.Items.Add(eee);
            }
        }
    }
    protected void btnSaveCancle_Click(object sender, EventArgs e)
    {
        if (CheckEmpty() == true)
        {
            Schedule schedule = new Schedule();
            schedule.Address = this.txtAddress.Text.Trim();
            schedule.BeginTime = Convert.ToDateTime(this.txtBegin.Text.Trim());
            schedule.CreateTime = Convert.ToDateTime(lbCreateDate.Text.Trim());
            schedule.CreateUser = this.lbCreateName.Text.Trim();
            schedule.EndTime = Convert.ToDateTime(this.txtEnd.Text.Trim());
            if (this.cbIfPublic.Checked == true)
            {
                schedule.IfPrivate = 0;
            }
            else
            {
                schedule.IfPrivate = 1;
            }
            MeetingInfo meet = new MeetingInfo();
            meet.MeetingName = this.ddlType.Text;
            meet.MeetingId = Convert.ToInt32(this.ddlType.SelectedValue);
            schedule.Meeting = meet;
            schedule.SchContent = txtContent.Text.Trim();
            schedule.Title = txtTitle.Text.Trim();

            if (Request.QueryString["id"] == null)
            {
                new ScheduleManager().AddSchedule(schedule);
                Response.Redirect("PersonalSchedule.aspx");
            }
            else
            {
                schedule.ScheduleId = Convert.ToInt32(Request.QueryString["id"]);
                new ScheduleManager().UpdateSchedule(schedule);
                Response.Redirect("PersonalSchedule.aspx");
            }

        }
        this.txtAddress.Text = "";
        this.txtBegin.Text = "";
        this.txtContent.Text = "";
        this.txtEnd.Text = "";
        this.txtTitle.Text = "";
        this.lbStfName.Items.Clear();
        this.cbIfPublic.Checked = false;
        this.ddlType.SelectedIndex = 0;
    }
    private bool CheckEmpty()
    {
        if (this.txtTitle.Text == "")
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('日程标题不能为空！')</script>");
            return false;
        }
        if (this.txtAddress.Text == "")
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('地址不能为空！')</script>");
            return false;
        }
        if (this.txtBegin.Text == "")
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('开始时间不能为空！')</script>");
            return false;
        }
        if (this.txtEnd.Text == "")
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('结束时间不能为空！')</script>");
            return false;
        }
        if (this.txtContent.Text == "")
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('内容不能为空！')</script>");
            return false;
        }


        if (this.ddlType.Text == "请选择")
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('您还没有选择会议类型！')</script>");
            return false;
        }
        if (this.lbStfName.Items.Count == 0)
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('预约人不能为空！')</script>");
            return false;
        }

        return true;
    }
    protected void btnDeleteName_Click(object sender, EventArgs e)
    {
        foreach (ListItem item in lbStfName.Items)
        {
            if (item.Selected == true)
            {
                new PreContractManager().DeletePreContract(Convert.ToInt32(item.Value));
            }
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(Request.QueryString["id"]);
        new ScheduleManager().DeleteSchdeule(id);
        this.txtAddress.Text = "";
        this.txtBegin.Text = "";
        this.txtContent.Text = "";
        this.txtEnd.Text = "";
        this.txtTitle.Text = "";
        this.lbStfName.Items.Clear();
        this.cbIfPublic.Checked = false;
        this.ddlType.SelectedIndex = 0;
        this.btnDelete.Enabled = false;
    }
    protected void btnCancle_Click(object sender, EventArgs e)
    {
        Response.Redirect("PersonalSchedule.aspx");
    }
}